﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TextView.ascx.cs" Inherits="JohnnySins.Controls.TextView" %>

<script type="text/javascript">
    var startListen = 0;

    $(function () {
        $("#textViewDialog").dialog({autoOpen: false, width: 640, minHeight: 480, resizable: false});
         $("#textViewDialog").parent().appendTo(jQuery("form:first"));
         $(".ui-dialog-titlebar").hide();
     });

     function showTextViewDialog(url, desc) {

         desc = (typeof desc === "undefined") ? "" : desc;

         var wWidth = $(window).width();
         var wHeight = $(window).height();

         var dlgWidth = 970;
         var dlgHeigth = wHeight * 99 / 100;

         var fld = $('#fieldsetText');
         fld.height(dlgHeigth - 50);
         var divDoc = $('#divtextView');
         divDoc.height(dlgHeigth - 100);

         var frameView = $('#frameDocument');
         frameView.attr('src', url);
         frameView.css('max-width', dlgWidth - 60);
         frameView.css('max-height', dlgHeigth - 120);
         
         var dlg = $("#textViewDialog");
         dlg.dialog("option", "width", dlgWidth);
         dlg.dialog("option", "height", dlgHeigth);
         dlg.dialog('open');

         $(document).on('click.textDialog', function () {
             if (startListen == 1) {
                 startListen = 0;
                 CloseTextView();
                 $(document).off('click.textDialog');
             }
             else
                 startListen = 1;
         });

         var arrPath = url.split('/');
         var fileName = arrPath[arrPath.length - 1];
         var nameFileWOExt = fileName.substr(0, fileName.lastIndexOf('.'));
         $("#<%= lnkDownload.ClientID %>").attr('href', 'DownloadArchive.aspx?t=2&f=' + encodeURIComponent(fileName));

         $("#<%= lnkDelete.ClientID %>").off('click');
         $("#<%= lnkDelete.ClientID %>").click(function () { DeleteTextFile(fileName) });

         $("#<%= divRename.ClientID %>").off('click');
         $("#<%= divRename.ClientID %>").click(function (e) {
             e.stopPropagation();
         })
         $("#<%= txtNewFileName.ClientID %>").val(desc);

         $("#<%= lnkRename.ClientID %>").off('click');
         $("#<%= lnkRename.ClientID %>").click(function () { RenameTextFile(fileName) });
     }

     function CloseTextView() {
         var frameView = $('#frameDocument');
         frameView.attr('src', '');
         $('#textViewDialog').dialog('close');
     }

     function DeleteTextFile(f) {

         if (!confirm('Please confirm if you want to delete file')) {
             startListen = 0;
             return;
         }

         $.ajax({
             type: "POST",
             url: '<%=ResolveClientUrl("~") %>Admin/DownloadArchive.aspx/DeleteFile',
             data: "{'t' : 2, 'f' : '" + encodeURIComponent(f) + "'}",
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (msg) {
                 var param = "";
                 if (selectedDate != "") {
                     var arrMonthYear = selectedDate.split(';');
                     param = String(parseInt(arrMonthYear[1]) - 1) + ';' + arrMonthYear[2];
                 }
                 RefreshCalendar(param);
             },
             error: function () {
                 alert('An Error has occured');
             }
         });
     }

     function RenameTextFile(f) {

         if (!confirm('Please confirm if you want to rename file')) {
             return;
         }

         var newName = $("#<%= txtNewFileName.ClientID %>").val();
         $.ajax({
             type: "POST",
             url: '<%=ResolveClientUrl("~") %>Admin/DownloadArchive.aspx/RenameFile',
             data: "{'t' : 2, 'f' : '" + encodeURIComponent(f) + "', 'n' : '" + encodeURIComponent(newName) + "'}",
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (msg) {
                 startListen = 0;
                 CloseTextView();
                 $(document).off('click.textDialog');

                 RefreshArchive(selectedDate);
             },
             error: function () {
                 alert('An Error has occured');
             }
         });
     }
</script>

<div id="textViewDialog">
    <div class="row">
        <div class="fieldset_block col-lg-12 page_area">
            <fieldset id="fieldsetText">
                <div class="fieldset_content form-horizontal col-lg-12" id="divtextView">
                            <table cellpadding="0" cellspacing="0" border="0" width="100%" style="height:inherit;">
                                <tr>
                                    <td align="center" valign="middle">
                                        <iframe src="" width="100%" height="100%" id="frameDocument">
                                        </iframe>
                                    </td>
                                </tr>
                            </table>
                </div>
                <div class="button_group">
                    <div class="col-lg-5">
                        <input type="button" class="btn btn-info" value="Close" onclick="CloseTextView(); return false;" />
                        <a runat="server" id="lnkDelete" class="btn btn-info" href="javascript:void(0)">Delete</a>
                        <a runat="server" id="lnkDownload" target="_blank" class="btn btn-info">Download</a>
                    </div>
                    <div class="col-lg-7" runat="server" id="divRename">
                        New name: <asp:TextBox runat="server" ID="txtNewFileName" />
                        <a runat="server" id="lnkRename" class="btn btn-info" href="javascript:void(0)">Rename</a>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
</div>

